# Copyright (c) 2021 Presto Labs Pte. Ltd.
# Author: gekim

import requests
from coin2.exchange.product_info import ProductInfoDefault
from coin2.exchange.symbology import SymbologyInfo


def generate():
  mea = 'Spot.Okex.v5'
  me = 'Spot.Okex'
  default = ProductInfoDefault()
  symbol_info = SymbologyInfo()
  data = requests.get('https://www.okx.com/api/v5/public/instruments?instType=SPOT').json()["data"]

  pis = []
  for info in data:
    if "state" not in info or info["state"] != "live":
      continue

    native_symbol = info['instId']
    native_base = info['baseCcy']
    native_quote = info['quoteCcy']
    base = symbol_info.native_to_norm_currency(mea, native_base)
    quote = symbol_info.native_to_norm_currency(mea, native_quote)
    symbol = "%s-%s" % (base, quote)

    pi = dict(
        symbol=symbol,
        base=base,
        quote=quote,
        native_symbol=native_symbol,
        native_base=native_base,
        native_quote=native_quote,
        price_ticksize=float(info['tickSz']),
        qty_ticksize=float(info['lotSz']),
        min_qty=float(info['minSz']),
    )
    d = default.product_info(me, symbol)
    if d['is_inverse']:
      import pdb
      pdb.set_trace()
    pi = {**pi, **default.product_info(me, symbol)}
    pis.append(pi)

  pis = sorted(pis, key=lambda x: x['symbol'])
  return pis


def gen_coin1_constants():
  data = generate()
  data = convert_coin1_constants(data)
  return data


def convert_coin1_constants(data):
  quote_currency_list = [info['native_quote'].upper() for info in data]
  base_currency_list = [info['native_base'].upper() for info in data]
  currency_list = base_currency_list + quote_currency_list
  symbol_list = [info['native_symbol'].lower().replace('-', '_') for info in data]
  return {
      'quote_currency_list': list(set(quote_currency_list)),
      'currency_list': list(set(currency_list)),
      'symbol_list': list(set(symbol_list)),
  }


if __name__ == "__main__":
  print(generate())
